<section class="content-header">
    <h1>
        <b>{$header}
            <small>>>{$desc}</small>
        </b>
    </h1>
</section>
<section class='content'>
    <div class="box">
        <div class="box-header">
            <div class="btn-group-sm">
              <a role ="button" href="{:url('Admin/createUser')}" class ="btn btn-primary"><b>添加管理员</b></a>
              <a class="pull-right btn btn-success btn-sm" onclick="location.reload()"> 刷新</a>
            </div>
        </div>
        <div class="box-body">
            <table class="table table-bordered table-hover">
                <thead>
                    <tr>
                        <th>序号</th>
                        <th>账户</th>
                        <th>级别</th>
                        <th>头像</th>
                        <th>所属用户组</th>
                        <th>电话</th>
                        <th>邮箱</th>
                        <th>QQ</th>
                        <th>微信号</th>
                        <th>登录IP</th>
                        <th>登录次数</th>
                        <th>上次登录时间</th>
                        <th>创建时间</th>
                        <th>是否在线</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    {foreach $data as $k=>$v}
                    <tr>
                        <td>{$k+1}</td>
                        <td>{$v['user']}</td>
                        <td>{$v['name']}</td>
                        <td><img src="/uploads/{$v['avatar']}" width="50px"/></td>
                        <td> 
                            {foreach $v.groups_text as $groupText}
                            <span class="label label-default">{$groupText}</span>
                            {/foreach}
                        </td> 
                        <td>{$v['phone'] ? : '暂无'}</td>
                        <td>{$v['email'] ? : '暂无'}</td>
                        <td>{$v['QQ'] ? : '暂无'}</td>
                        <td>{$v['wechat'] ? : '暂无'}</td>
                        <td>{$v['last_login_ip']}</td>
                        <td>{$v['login_times']}</td>
                        <td>{$Think.session.b_uinfo['last_login_time'] ? : '未登录'}</td>
                        <td>{$v['create_at']}</td>
                        <td>
                            {eq name="$v['line_status']" value="1"}
                            <span class="label label-success label-sm" style="cursor: pointer;">在线</span>
                            {else /}
                            <span class="label label-danger label-sm" style="cursor: pointer;">离线</span>
                            {/eq}
                        </td>
                        <td>
                            {eq name="$v['status']" value="1"}
                            <span class="label label-success" onclick="handle.changeStatus(this, {$v.id}, 0)" style="cursor: pointer;">正常</span>
                            {else /}
                            <span class="label label-danger" onclick="handle.changeStatus(this, {$v.id}, 1)" style="cursor: pointer;">禁用</span>
                            {/eq}
                        </td>
                        <td>
                            <!-- 编辑按钮 -->
                            <a role="button" class="btn btn-primary btn-sm" href="{:url('createUser',['id'=>$v['id']])}" ><i class="fa fa-pencil"></i></a>
                            {gt name="$v['id']" value="1"}
                            <button type="button" class="btn btn-danger btn-sm" onclick="handle.delUser(this,{$v.id})"><i class="fa fa-trash-o"></i></button>
                            {/gt}
                        </td>
                    </tr>
                    {/foreach}
                </tbody>
            </table>
        </div>
    </div>
</section>
<script>
    var requestUrl = {
        changeStatus : "{:url('Admin/setUserStatus')}",
        delUser      : "{:url('Admin/userDelete')}"
    };
    
    var handle = {
        changeStatus : function(obj, id, status) {
            $.post(requestUrl.changeStatus, {id:id, status:status}, function(e) {
                if (e.code) {
                    if (status) { //禁用
                        $(obj).parent('td').html('<span class="label label-success" onclick ="handle.changeStatus(this,'+id+', 0)" style="cursor: pointer;">正常</span>');
                    } else { //正常
                        $(obj).parent('td').html('<span class="label label-danger" onclick="handle.changeStatus(this, '+id+', 1)" style="cursor: pointer;">禁用</span>');
                    }
                } else {
                    bayMax.warning(e.msg);
                }
            })
        },
        delUser : function(obj, id){
            $.post(requestUrl.delUser, {id:id, status:status}, function(e) {
                if (e.code) {
                    bayMax.msg(e.msg, function(){
                        $(obj)
                    });
                } else {
                    bayMax.warning(e.msg);
                }
            })
        }
    };
</script>